#' gsaanno_simplify UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd
#'
#' @importFrom shiny NS tagList
mod_gsaanno_simplify_ui <- function(id){
  ns <- NS(id)
  tagList(
    helpText("This step will take several minutes. Please wait..."),
    shinyWidgets::sliderTextInput(
      inputId = ns("pvalue"),
      label = "Choose a pvalue:",
      choices = seq(from = 0.05,
                    to = 0.01,
                    by = -0.01),
      grid = TRUE
    ),
    shinyWidgets::sliderTextInput(
      inputId = ns("qvalue"),
      label = "Choose a qvalue:",
      choices = seq(from = 0.05,
                    to = 0.01,
                    by = -0.01),
      grid = TRUE
    ),
    shinyWidgets::prettyRadioButtons(
      inputId = ns("ont"),
      label = "Choices Subontologies",
      choices = c("BP" = "BP", "MF" = "MF", "CC" = "CC"),
      selected = "BP",
      icon = icon("eye-dropper"),
      bigger = TRUE,
      inline = TRUE,
      fill = TRUE,
      plain = TRUE,
      status = "success",
      animation = "pulse"
    ),
    shinyWidgets::materialSwitch(
      inputId = ns("sim"),
      label = "Simplify",
      value = TRUE,
      status = "success"
    ),
    shinycustomloader::withLoader(
      plotOutput(ns("gobar")),
      type = "html",
      loader = "dnaspin"
    )
  )
}

#' gsaanno_simplify Server Functions
#'
#' @noRd
mod_gsaanno_simplify_server <- function(id,ENTREZID=""){
  moduleServer( id, function(input, output, session){
    ns <- session$ns

    go <- reactive({
      ENTREZID <- ENTREZID$ENTREZID()
      clusterProfiler::enrichGO(ENTREZID, OrgDb = "org.Hs.eg.db", ont=input$ont,
                                      pvalueCutoff = input$pvalue,qvalueCutoff = input$qvalue)
    })

    sgo <- reactive({
      go <- go()
      if (input$sim) {
        clusterProfiler::simplify(go)
      } else {
        go
      }
    })

    output$gobar <- renderPlot({
      barplot(sgo(), font.size =10)
    })

  })
}

## To be copied in the UI
# mod_gsaanno_simplify_ui("gsaanno_simplify_ui_1")

## To be copied in the server
# mod_gsaanno_simplify_server("gsaanno_simplify_ui_1")
